package thread;

/**
 * 线程方法练习
 */
public class ThreadMethodExercise {

    public static void main(String[] args) throws InterruptedException {

        /*
            1)主线程每隔1s 输出 hi 一共10次
            2)当输出到 hi 5 时，启动一个子线程(要求实现Runnable)，每隔1s输出 hello
              等该线程输出10次 hello 后 退出
            3)主线程继续输出hi 直到主线程退出
         */
        Tme tme = new Tme();
        Thread thread = new Thread(tme);
        for (int i = 1; i <= 10; i++) {
            System.out.println("hi " + i);
            Thread.sleep(1000);
            if (i == 5) {
                thread.start();//启动子线程
                thread.join();
                System.out.println("子线程结束...");
            }
        }
        System.out.println("主线程结束...");
    }
}

class Tme implements Runnable{

    private int count = 0;

    @Override
    public void run() {
        while (true){
            System.out.println("hello " + (++count));
            try {
                Thread.sleep(1000);//休眠1s
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count == 10){
                break;
            }
        }
    }

}

